// 1.引入基础模块
const express = require("express");
const axios = require("axios");
// 2.获取路由实例
const router = express.Router();
// 3.定义路由规则
// 3.1获取分类页面
router.get("/cate", function (req, res) {
  res.render("cate");
});

// 3.2获取顶级全部的商品分类数据
router.get("/catetop", async function (req, res) {
  let result = await axios({
    url: "http://ajax-api.itheima.net/api/category/top",
  });
  res.send({ code: 200, msg: "顶级分类请求成功！", data: result.data });
});

// 3.3根据一级分类的id获取二级商品分类数据
router.get("/catesub", async function (req, res) {
  // 获取顶级分类的id
  let { id = "" } = req.query;
  if (id == "") {
    res.send({ code: 501, msg: "缺少顶级分类id", data: null });
  }
  let result = await axios({
    url: `http://ajax-api.itheima.net/api/category/sub?id=${id}`,
  });
  res.send({ code: 200, msg: "二级分类请求成功！", data: result.data });
});
// 4.导出路由实例
module.exports = router;
